// 输入小数
export const inputDecimalData = (val, limit, intlimit) => {
	var str = val;
	var len1 = str.substr(0, 1);
	var len2 = str.substr(1, 1);
	// 如果第一位是0，第二位不是点，就用数字把点替换掉
	if (str.length > 1 && len1 === 0 && len2 !== ".") {
		str = str.substr(1, 1);
	}
	// 第一位不能是.
	if (len1 === ".") {
		str = "";
	}
	// 限制只能输入一个小数点
	if (str.indexOf(".") !== -1) {
		var str_ = str.substr(str.indexOf(".") + 1);
		if (str_.indexOf(".") !== -1) {
			str = str.substr(0, str.indexOf(".") + str_.indexOf(".") + 1);
		}
	}

	// 正则替换
	str = str.replace(/[^\d^.]+/g, ""); // 保留数字和小数点
	if (limit / 1 === 1) {
		str = str.replace(/^\D*([0-9]\d*\.?\d{0,1})?.*$/, "$1"); // 小数点后只能输 1 位
	} else {
		str = str.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/, "$1"); // 小数点后只能输 2 位
	}
	//整数部分
	if (str.length > 0 && intlimit > 0) {
		let _strArr = str.split(".");
		let dotIndex = str.indexOf(".");
		let intPart = _strArr[0] !== "" ? `${parseInt(_strArr[0])}` : "0";
		if (intPart !== "") {
			str = `${
        intPart.length > intlimit ? intPart.slice(0, intlimit) : intPart
      }${dotIndex !== -1 ? "." : ""}${_strArr[1] ? `${_strArr[1]}` : ""}`;
		}
	}

	return str;
};

export const getOauthProvider = () => {
	return new Promise((resolve, reject) => {
		let weixinSupport = false;
		let appleSupport = false;
		let univerifySupport=false;
		let mobileSystem = uni.getSystemInfoSync();
		uni.getProvider({
			service: 'oauth',
			success: function(res) {
				console.log(res,">>>res")
				for (var i = 0; i < res.provider.length; i++) {
					let providerItem = res.provider[i].toLowerCase();
					if (providerItem == 'weixin') {
						weixinSupport = true
					} // platform == 'ios' && system >= '13'
					if (mobileSystem.platform == 'ios' && mobileSystem.system >= '13' &&
						providerItem == 'apple') {
						appleSupport = true
					}
					if(providerItem=='univerify'){
						univerifySupport=true
					}
				}
				resolve({
					weixinSupport,
					appleSupport,
					univerifySupport
				})
			}

		})
	})
}
// 第三方登录、微信、apple
export const thirdLogin = (serverOptions) => {
	return new Promise((resolve, reject) => {
		uni.login({
			...serverOptions,
			success: (res) => {
				console.log(res,">>>")
				resolve(res)
			},
			fail: (err) => {
				reject(err)
			}
		})
	})

}